package com.hcxy.filetransform.adapter;

import java.util.ArrayList;

import com.hcxy.filetransform.R;
import com.hcxy.filetransform.domain.DeviceInfo;
import com.hcxy.filetransform.widget.CircleImage;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DeviceListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<DeviceInfo> datas;
	private LayoutInflater layoutInflater;                                                         
	
	private ArrayList<Integer> msgNums = new ArrayList<Integer>();
	public DeviceListAdapter(Context context,ArrayList<DeviceInfo> datas) {
		this.context = context;
		this.datas = datas;
		initNum(datas);
		layoutInflater = LayoutInflater.from(context);
	}
	
	public void initNum(ArrayList data){
		for(int i=0;i<data.size();i++){
			msgNums.add(0);
		}
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public void addDeviceInfo(DeviceInfo info){
		datas.add(info);
		msgNums.add(0);
		notifyDataSetChanged();
	}

	public void addMsgNum(DeviceInfo from){
		int position = getPostionByInfo(from);
		if (position!=-1) {
			int old = msgNums.get(position);
			msgNums.set(position, old+1);
			notifyDataSetChanged();
		}
	}
	
	public void clearNum(int postion){
		msgNums.set(postion, 0);
	}
	
	public int getPostionByInfo(DeviceInfo from){
		for(int i=0;i<datas.size();i++){
			DeviceInfo f = datas.get(i);
			if (f.getMac().equals(from.getMac())) {
				return i;
			}
		}
		return -1;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView==null) {
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.item_devices_list, null);
			ViewUtils.inject(holder, convertView);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		DeviceInfo dinfo = datas.get(position);
		int type = dinfo.getType();
		if(type==DeviceInfo.DeviceType.COMPUTER){
			holder.cimage.setImageResource(R.drawable.computer);
		}else{
			holder.cimage.setImageResource(R.drawable.android);
		}
		holder.txName.setText(dinfo.getName());
		holder.txIP.setText(dinfo.getIp());
		int num = msgNums.get(position);
		if (num>0) {
			holder.txMsgNum.setVisibility(View.VISIBLE);
			holder.txMsgNum.setText(num+"");
		}else{
			holder.txMsgNum.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}
	class ViewHolder{
		@ViewInject(R.id.ci_device_type)
		public CircleImage cimage;
		@ViewInject(R.id.tx_device_ip)
		public TextView txIP;
		@ViewInject(R.id.tx_device_name)
		public TextView txName;
		@ViewInject(R.id.tx_main_msg_num)
		public TextView txMsgNum;
	}
}
